<?php

namespace app\admin\controller;
use app\admin\model\NoteModel;
use app\admin\model\UserModel;
use think\Db;

class Note extends Base
{

    /**
     * [index 留言列表]
     * @author 【朱沪宁】
     */
    public function index(){
        $map = [];
        $rolename=session('rolename');
        $this->assign('showBut',$rolename=='超级管理员'?1:0);

        $user = input('user');
        if($user&&$user!=="")
        {
            $map['b.nickname'] = ['like',"%" . $user . "%"];
        }
        $to_user = input('to_user');
        if($to_user&&$to_user!=="")
        {
            $map['c.nickname'] = ['like',"%" . $to_user . "%"];
        }
        $Nowpage = input('get.page') ? input('get.page'):1;
        $limits = config('list_rows');// 获取总条数
        $count = Db::name('note')
            ->join('think_member b', 'think_note.user_id = b.id')
            ->join('think_member c', 'think_note.to_user_id = c.id')
            ->where($map)->count();//计算总页面
        $allpage = intval(ceil($count / $limits));
        $note = new NoteModel();
        $lists = $note->getNoteByWhere($map, $Nowpage, $limits);
        $this->assign('Nowpage', $Nowpage); //当前页
        $this->assign('allpage', $allpage); //总页数
        $this->assign('count', $count); 
        $this->assign('user', $user);
        $this->assign('to_user', $to_user);

        if(input('get.page')){
            return json($lists);
        }
//        print_r($lists);die;
        return $this->fetch();
    }

    /**
     * [del_note 删除留言]
     * @return [type] [description]
     * @author 【朱沪宁】
     */
    public function del_note()
    {
        $id = input('param.id');
        $cate = new NoteModel();
        $flag = $cate->delNote($id);
        return json(['code' => $flag['code'], 'data' => $flag['data'], 'msg' => $flag['msg']]);
    }

}